/**
 * 表字段创建器
 * @author xueyanjun
 */
class ColumnCreator{
	constructor(name){
		this.name = name;
		// [name, type, pk, not null, default]
		this.arr = ['"'+name+'"'];
	}
	
	type(name,length){
		let typeText = name;
		if(length != null){
			typeText  = typeText + `(${length})`
		}
		this.arr[1] = typeText;
		this.typeText = typeText;
		return this;
	}
	
	pk(){
		this.pkText = 'PRIMARY KEY'
		this.arr[2] = this.pkText;
		return this;
	}
	
	pkAutoIncr(){
		this.type('INTEGER');
		this.pkText = 'PRIMARY KEY AUTOINCREMENT'
		this.arr[2] = this.pkText;
		return this;
	}
	
	notNull(){
		this.notNullText = 'NOT NULL'
		this.arr[3] = this.notNullText;
		return this;
	}
	
	defaultVal(val){
		this.defaultValText = 'DEFAULT '+val;
		this.arr[4] = this.defaultValText;
		return this;
	}
	
	getSQL(){
		const arr = this.arr;
		let text = '';
		for (let s of arr) {
			if(s){
				text += ' '+s;
			}
		}
		return text;
	}
}

export default ColumnCreator;